package com.macrosolution.exdebito


import static org.springframework.http.HttpStatus.*
import grails.transaction.Transactional

import grails.converters.JSON
import grails.plugin.springsecurity.annotation.Secured;
/**
 * AnomaliaBancariaController
 * A controller class handles incoming web requests and performs actions such as redirects, rendering views and so on.
 */
	@Secured(['ROLE_ADMIN','ROLE_AMM','ROLE_CC','ROLE_AVV','ROLE_C','ROLE_SC','ROLE_BCKOFF'])
class AnomaliaBancariaController {

    static allowedMethods = [save: "POST", update: "PUT", delete: "DELETE"]

	def index(Integer max) {
        params.max = Math.min(max ?: 10, 100)
        respond AnomaliaBancaria.list(params), model:[anomaliaBancariaInstanceCount: AnomaliaBancaria.count()]
    }

	def list(Integer max) {
        params.max = Math.min(max ?: 10, 100)
        respond AnomaliaBancaria.list(params), model:[anomaliaBancariaInstanceCount: AnomaliaBancaria.count()]
    }

    def show(AnomaliaBancaria anomaliaBancariaInstance) {
        respond anomaliaBancariaInstance
    }

    def create() {
        respond new AnomaliaBancaria(params)
    }

    @Transactional
    def save(AnomaliaBancaria anomaliaBancariaInstance) {
        if (anomaliaBancariaInstance == null) {
            notFound()
            return
        }

        if (anomaliaBancariaInstance.hasErrors()) {
            respond anomaliaBancariaInstance.errors, view:'create'
            return
        }

        anomaliaBancariaInstance.save flush:true

        request.withFormat {
            form {
                flash.message = message(code: 'default.created.message', args: [message(code: 'anomaliaBancariaInstance.label', default: 'AnomaliaBancaria'), anomaliaBancariaInstance.id])
                redirect anomaliaBancariaInstance
            }
            '*' { respond anomaliaBancariaInstance, [status: CREATED] }
        }
    }

    def edit(AnomaliaBancaria anomaliaBancariaInstance) {
        respond anomaliaBancariaInstance
    }

    @Transactional
    def update(AnomaliaBancaria anomaliaBancariaInstance) {
        if (anomaliaBancariaInstance == null) {
            notFound()
            return
        }

        if (anomaliaBancariaInstance.hasErrors()) {
            respond anomaliaBancariaInstance.errors, view:'edit'
            return
        }

        anomaliaBancariaInstance.save flush:true

        request.withFormat {
            form {
                flash.message = message(code: 'default.updated.message', args: [message(code: 'AnomaliaBancaria.label', default: 'AnomaliaBancaria'), anomaliaBancariaInstance.id])
                redirect anomaliaBancariaInstance
            }
            '*'{ respond anomaliaBancariaInstance, [status: OK] }
        }
    }

    @Transactional
    def delete(AnomaliaBancaria anomaliaBancariaInstance) {

        if (anomaliaBancariaInstance == null) {
            notFound()
            return
        }

        anomaliaBancariaInstance.delete flush:true

//        request.withFormat {
//            form {
//                flash.message = message(code: 'default.deleted.message', args: [message(code: 'AnomaliaBancaria.label', default: 'AnomaliaBancaria'), anomaliaBancariaInstance.id])
//                redirect action:"index", method:"GET"
//            }
//            '*'{ render status: NO_CONTENT }
//        }
    }

	def savePerito() {
		String message
		println params.perito_id
		println params.id
		Pratica praticaInstance = Pratica.get(params.getLong('id'))
		if (params.perito_id && params.perito_id!="-1"){
			AnomaliaBancaria ab = praticaInstance.anomaliaBancaria
			ab.setPerito(Perito.get(params.getLong('perito_id')))
			ab.save(flush:true)
		} else {
			if(praticaInstance.anomaliaBancaria.perito){
				AnomaliaBancaria ab = praticaInstance.anomaliaBancaria
				ab.perito=null
				ab.save(flush:true)
			}
		}
		message="{'message':'Perito assegnato correttamente alla Pratica'}"
		render JSON.parse(message) as JSON
		
//		redirect controller:"pratica",action:"anomaliabancaria",id:praticaInstance.identifier
	}
	
    protected void notFound() {
        request.withFormat {
            form {
                flash.message = message(code: 'default.not.found.message', args: [message(code: 'anomaliaBancariaInstance.label', default: 'AnomaliaBancaria'), params.id])
                redirect action: "index", method: "GET"
            }
            '*'{ render status: NOT_FOUND }
        }
    }
}
